

use std::str::FromStr;

use regex::Regex;

use crate::util::log_helper::push_error;

pub fn get_naive_date(date_str: &str) -> Option<chrono::NaiveDate> {
  let re = Regex::new(r"(\d{2,4})-(\d{1,2})-(\d{1,2})").expect("cannot use regex");
  let captures_option = re.captures(date_str);
  if captures_option.is_none() {
    return None;
  }
  let captures = captures_option.unwrap();
  let target_string = captures.get(0).map_or("", |m| {
    return m.as_str();
  });
  let date_result = chrono::NaiveDate::from_str(target_string);
  if date_result.is_err() {
    push_error(
      format!("cannot to naiveDate: [{}]", target_string)
    );
    return None;
  }
  return Some(date_result.unwrap());
}
